package com.minggo.charmword.logic;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import android.os.AsyncTask;
import android.os.Handler;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.minggo.charmword.model.Around;
import com.minggo.charmword.model.Result;
import com.minggo.charmword.net.ApiUrl;
import com.minggo.charmword.net.ApiUtil;

/**
 * 周围
 * @author minggo
 * @date 2014-10-8 上午11:42:06
 */
public class AroundWordUtil extends AsyncTask<Void, Void, List<Around>> {

	private double longitude;
	private double altitude;
	private Handler handler;
	
	public static final int GET_AROUND = 10004;

	public AroundWordUtil(double longitude,double altitude,Handler handler){
		this.longitude = longitude;
		this.altitude = altitude;
		this.handler = handler;
	}
	
	@Override
	protected List<Around> doInBackground(Void... param) {
		Map<String, Object> params = ApiUtil.addRequiredParam();
		params.put("longitude", longitude);
		params.put("altitude", altitude);
		Result result = ApiUtil.getResult(ApiUrl.GET_AROUND, params, false, null);
		if (null == result) {
			return null;
		}
		try {
			Type listType = new TypeToken<List<Around>>() {}.getType();
			List<Around> around = new Gson().fromJson(result.content, listType);
			if (around!=null&&!around.isEmpty()) {
				return around;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void onPostExecute(List<Around> result) {
		super.onPostExecute(result);
		handler.obtainMessage(GET_AROUND, result).sendToTarget();
	}
	
}
